package com.dev;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.fb.FileBrowserPage;
import java.io.File;
class IndentFileItem extends LinearLayout implements OnClickListener,OnLongClickListener {
	LinearLayout primary;
	IconView ivtype;
	IconViewSpread ivs;
	LinearLayout linear;
	TextView name;
	TextView info;
	File file;
	File[] file_listed;
	boolean b;
	ColorDrawable sed=new ColorDrawable(0x77ffff00);
	FileBrowserPage.Pressed pressed=new FileBrowserPage.Pressed();
	IconLoader iconLoader;
	int index;
	IndentFileItem(MainActivity ma){
		super(ma);
		setOrientation(VERTICAL);
		//setLayoutParams(indentItemlp);
		ivtype=new IconView(ma);
		ivtype.setOnClickListener(this);
		name=new TextView(ma);
		name.setTypeface(Typeface.MONOSPACE);
		name.setTextSize(TypedValue.COMPLEX_UNIT_DIP,11);
		name.setTextColor(0xff000000);
		name.setPadding(12,0,0,0);
		info=new TextView(ma);
		info.setTextSize(TypedValue.COMPLEX_UNIT_DIP,10);
		info.setTextColor(0xff888888);
		info.setPadding(12,0,0,0);
		linear=new LinearLayout(ma);
		linear.setOrientation(VERTICAL);
		linear.addView(name,new LayoutParams(-1,0,5));
		linear.addView(info,new LayoutParams(-1,0,4));
		ivs=new IconViewSpread(ma);
		ivs.setOnClickListener(this);
		ivs.setOnLongClickListener(this);
		primary=new LinearLayout(ma);
		primary.setOrientation(HORIZONTAL);
		primary.addView(ivtype);
		primary.addView(linear,new LayoutParams(0,-1,1f));
		primary.addView(ivs);
		//addView(primary,itemlp);
	}
	void setFile(File file,int index){
		this.file=file;
		this.index=index;
		String name=file.getName();
		this.name.setText(name);
		/*if(selected!=null){
			if(selected[index]){
				primary.setBackground(sed);
			}else{
				primary.setBackground(pressed);
			}
		}else{
			primary.setBackground(pressed);
		}*/
		if(name.charAt(0)=='.'){
			//ivtype.setColorFilter(hidden);
		}else{
			ivtype.setColorFilter(null);
		}
		if(file.exists()){
			this.name.setTextColor(0xff000000);
			if(file.isDirectory()){
				if(iconLoader!=null){
					iconLoader.interrupt();
					iconLoader=null;
				}
				ivtype.setImageResource(R.drawable.folder);
				if(file.canRead()){
					file_listed=file.listFiles();
					if(file_listed.length==0){
						info.setText("空目录");
					}else{
						info.setText("共有"+file_listed.length+"项");
					}
				}else{
					info.setText("不可读");
				}
			}else{
				long length=file.length();
				if(length>1024*1024){
					info.setText(length/1024/1024+"MB");
				}else if(length>1024){
					info.setText(length/1024+"KB");
				}else{
					info.setText(length+"B");
				}
				int dot=name.lastIndexOf('.');
				if(dot<0){
					if(iconLoader!=null){
						iconLoader.interrupt();
						iconLoader=null;
					}
					//ivtype.setImageBitmap(typefile);
				}else{
					String type=name.substring(dot+1);
					switch(type){
						case "c":
							//ivtype.setImageBitmap(typec);
							if(iconLoader!=null){
								iconLoader.interrupt();
								iconLoader=null;
							}
							break;
						case "cpp":
							//ivtype.setImageBitmap(typecpp);
							if(iconLoader!=null){
								iconLoader.interrupt();
								iconLoader=null;
							}
							break;
						case "txt":
							//ivtype.setImageBitmap(typetxt);
							if(iconLoader!=null){
								iconLoader.interrupt();
								iconLoader=null;
							}
							break;
						case "tar":
							//ivtype.setImageBitmap(typearchive);
							if(iconLoader!=null){
								iconLoader.interrupt();
								iconLoader=null;
							}
							break;
						case "zip":
							//ivtype.setImageBitmap(typearchive);
							if(iconLoader!=null){
								iconLoader.interrupt();
								iconLoader=null;
							}
							break;
						case "apk":
							/*if(icons[index]!=null){
								ivtype.setImageBitmap(icons[index]);
							}else{
								ivtype.setImageBitmap(typeApk);
							}*/
							break;
						/*case "png":
							if(icons[index]!=null){
								ivtype.setImageBitmap(icons[index]);
							}else{
								ivtype.setImageBitmap(typeImage);
							}
							break;
						case "jpg":
							if(icons[index]!=null){
								ivtype.setImageBitmap(icons[index]);
							}else{
								ivtype.setImageBitmap(typeImage);
							}
							break;
						case "gif":
							if(icons[index]!=null){
								ivtype.setImageBitmap(icons[index]);
							}else{
								ivtype.setImageBitmap(typeImage);
							}
							break;
						case "pdf":
							ivtype.setImageBitmap(typePdf);
							if(iconLoader!=null){
								iconLoader.interrupt();
								iconLoader=null;
							}
							break;
						case "mp3":
							ivtype.setImageResource(R.drawable.ic_music);
							break;
						case "m4a":
							ivtype.setImageResource(R.drawable.ic_music);
							break;
						case "java":
							ivtype.setImageResource(R.drawable.file_type_java);
							break;
						case "svg":
							ivtype.setImageResource(R.drawable.file_type_svg);
							break;
						case "html":
							ivtype.setImageResource(R.drawable.file_type_html);
							break;
						default:
							ivtype.setImageBitmap(typefile);
							if(iconLoader!=null){
								iconLoader.interrupt();
								iconLoader=null;
							}*/
					}
				}
			}
		}else{
			this.name.setTextColor(0x77888888);
		}
	}
	@Override
	public void onClick(View view) {
		if(view==ivs){
			if(ivs.isSelected()){
				ivs.setSelected(false);
			}else{
				ivs.setSelected(true);
			}
		}
	}
	@Override
	public boolean onLongClick(View view) {
		return true;
	}
	class IconLoader extends Thread{
		final int index;
		final File file;
		IconLoader(){
			index=IndentFileItem.this.index;
			file=IndentFileItem.this.file;
		}
	}
}
